package com.example.demo01.Service.mobileService;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * 地理编码服务
 */
@RestController
public class GeoCodeService {

    //?ak=&output=&=&=35.186768,113.252951

    /***
     * 地理反向编码工具，格局经纬度坐标获取地理位置
     * @param lng
     * @param lat
     * @return
     */
    @RequestMapping("/geolocation/GET/bylonlat")
    public String getLocationByLngLat(
           @RequestParam(value = "lng") String lng,
            @RequestParam(value = "lat") String lat
    ){
        CloseableHttpClient httpClient=null;//客户端
        CloseableHttpResponse httpResponse=null;//响应体
        HttpGet httpGet=null;
        URIBuilder uriBuilder=null;
        String result="";
        //异常处理
        try {
            ///实例化资源路径
            uriBuilder=new URIBuilder("http://api.map.baidu.com/reverse_geocoding/v3/");
            uriBuilder.setParameter("ak","hRjmeYDnl4y1BsRHvmEvNueKiAY0lPeW");
            uriBuilder.setParameter("output","json");
            uriBuilder.setParameter("coordtype","wgs84ll");
            uriBuilder.setParameter("location",lat+","+lng);
            System.out.println(lat+","+lng);
            //实例化httpget
            httpClient=HttpClients.createDefault();
            httpGet=new HttpGet(uriBuilder.build());
            //设置请求头
            httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4)");
            //发起get请求
            httpResponse=httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode()==200){
                result = EntityUtils.toString(httpResponse.getEntity(),"utf8");
                System.out.println(result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                result=JSONObject.parseObject(jsonObject.get("result").toString()).get("formatted_address").toString();
                System.out.println(result);
                return result;
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpResponse!=null)
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (httpClient!=null)
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return result;
    }

    @Test
    public void test1(){
               //
//        String str="{\"status\":0,\"result\":{\"location\":{\"lng\":35.18638514962431,\"lat\":85.09099187480457},\"formatted_address\":\"\",\"business\":\"\",\"addressComponent\":{\"country\":\"\",\"country_code\":-1,\"country_code_iso\":\"\",\"country_code_iso2\":\"\",\"province\":\"\",\"city\":\"\",\"city_level\":2,\"district\":\"\",\"town\":\"\",\"town_code\":\"\",\"adcode\":\"0\",\"street\":\"\",\"street_number\":\"\",\"direction\":\"\",\"distance\":\"\"},\"pois\":[],\"roads\":[],\"poiRegions\":[],\"sematic_description\":\"\",\"cityCode\":0}}";
//        JSONObject jsonObject = JSONObject.parseObject(str);
//        JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("result").toString());
//        System.out.println(jsonObject1);
       // System.out.println(jsonObject);
        System.out.println(getLocationByLngLat("113.252951","35.186768" ));
    }

}
